<?php namespace testsacvoyage\storage;

use \ArrayIterator;

require_once 'testsacvoyage/storage/AbstractTest.php';

require_once 'sacvoyage/storage/Collection.php';
use \sacvoyage\storage\Collection;

require_once 'sacvoyage/Object.php';
use \sacvoyage\Object;




class CollectionTest extends AbstractTest {

	public function setUp() {
		$this->resetDatabase();
		$this->getGateway()->query("CREATE TABLE sacvoyage_alex_collections (
	name TINYTEXT NOT NULL,
	actual_table TINYTEXT NOT NULL,
	history_table TINYTEXT NOT NULL,
	UNIQUE(name(100)),
	UNIQUE(actual_table(100)),
	UNIQUE(history_table(100)),
	INDEX(name(100))
) DEFAULT CHARSET=utf8 COLLATE=utf8_bin");

	}
	
	public function _testAll() {
		$collection=new Collection($this->getGateway(), 'sacvoyage_alex_', 'db.notes');
		$collection->write(new Object(array('_id'=>'newnote', '_rev'=>1, 'param'=>'value1')));
		sleep(1);
		$collection->write(new Object(array('_id'=>'newnote', '_rev'=>8, 'param'=>'value8')));
		sleep(1);
		$collection->write(new Object(array('_id'=>'newnote', '_rev'=>5, 'param'=>'value5')));
		
		$result=$collection->find();
		$this->assertEquals(array(new Object(array('_id'=>'newnote', 'param'=>'value8'))), $result);
				
		sleep(1);
		$collection->write(new Object(array('_id'=>'newnote', 'param'=>'value666')));
		$this->assertEquals(array(new Object(array('_id'=>'newnote', 'param'=>'value666'))), $result);
		
		
		
		
		
	}
	
	
	/**
	 * @expectedException	Exception
	 */
	public function _testParams() {
		$collection=new Collection($this->getGateway(), 'sacvoyage_alex_', 'db.notes');
		$collection->find('alex');
	}

	/*
	 * 
	 */
	public function _testOverwriteGivenVersion() {
		$collection=new Collection($this->getGateway(), 'sacvoyage_alex_', 'db.notes');
		$collection->write(new Object(array('_id'=>'newnote', '_rev'=>1, 'param'=>'value1')));
		$collection->write(new Object(array('_id'=>'newnote', '_rev'=>3, 'param'=>'value3')));
		$collection->write(new Object(array('_id'=>'newnote', '_rev'=>2, 'param'=>'value2')));
		$result=$collection->find();
		//$this->assertEquals(array(new Object(array('_id'=>'newnote', 'param'=>'value3'))), $result);
	}
	
	/*
	 * @depends testOverwriteGivenVersion
	 */
	public function testMarkAsDeleted() {
		$collection=new Collection($this->getGateway(), 'sacvoyage_alex_', 'db.notes');
		$collection->write(new Object(array('_id'=>'newnote', 'param'=>'value1')));
		$collection->write(new Object(array('_id'=>'newtask', 'param'=>'value1')));
		sleep(1);
		
		$collection->write(new Object(array('_id'=>'newnote', 'param'=>'value2')));
		$collection->write(new Object(array('_id'=>'newtask', 'param'=>'value2')));
		sleep(1);
		
		$collection->delete(new Object(array('filter'=>new Object(array('_id'=>'newnote')))));

		
		$result=$collection->find();
		$this->assertEquals(
			array(
				new Object(array('_id'=>'newtask', 'param'=>'value2'))
			),
			$result
		);
		
		$result=$collection->find(new Object(array(
			'history'=>true,
			'order'=>array(
				new Object(array(
					'_id'=>'asc',
				)),
				new Object(array(
					'param'=>'asc',
				))		
			)
		)));

		$this->assertEquals(
			array(
				new Object(array('_id'=>'newnote', '_deleted'=>true )),
				new Object(array('_id'=>'newnote', 'param'=>'value1')),
				new Object(array('_id'=>'newnote', 'param'=>'value2')),
				new Object(array('_id'=>'newtask', 'param'=>'value1')),
				new Object(array('_id'=>'newtask', 'param'=>'value2'))
			),
			$result
		);
		
	}
	
	/*
	 * @depends testMarkAsDeleted
	 */
	public function _testOverwriteWithoutVersion() {
		$collection=new Collection($this->getGateway(), 'sacvoyage_alex_', 'db.notes');
		$collection->write(new Object(array('_id'=>'newnote', 'param'=>'value1')));
		sleep(1);
		$collection->write(new Object(array('_id'=>'newnote', 'param'=>'value2')));
		sleep(1);
		$collection->write(new Object(array('_id'=>'newnote', 'param'=>'value3')));
		$result=$collection->find();
		$this->assertEquals(array(new Object(array('_id'=>'newnote', 'param'=>'value3'))), $result);
	}
	




}

?>
